package com.woniuxy.joylifemarket.service.impl;

import com.woniuxy.common.entity.CenterUser;
import com.woniuxy.common.entity.MarketAttention;
import com.woniuxy.common.mapper.CenterUserMapper;
import com.woniuxy.common.mapper.MarketAttentionMapper;
import com.woniuxy.common.utils.KeyUtil;
import com.woniuxy.joylifemarket.service.MarketAttentionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MarketAttentionServiceImpl implements MarketAttentionService {
    @Autowired
    MarketAttentionMapper marketAttentionMapper;
    @Autowired
    CenterUserMapper centerUserMapper;

    @Override
    public List<MarketAttention> findAllAttentionByUserId(String userid) {
        List<MarketAttention> allAtention = marketAttentionMapper.findAllAtentionByUserId(userid);
        for (MarketAttention marketAttention:allAtention) {
            CenterUser receiveUser = centerUserMapper.selectByPrimaryKey(marketAttention.getReceiveuserId());
            marketAttention.setReceiveuser(receiveUser);
        }
        return allAtention;
    }

    @Override
    public List<MarketAttention> findAllFansByReceiveUserId(String receiveUserid) {
        List<MarketAttention> allFans = marketAttentionMapper.findAllFansByReceiveUserId(receiveUserid);
        for (MarketAttention marketAttention:allFans) {
            CenterUser user = centerUserMapper.selectByPrimaryKey(marketAttention.getUserId());
            marketAttention.setUser(user);
        }
        return allFans;
    }

    @Override
    public Boolean findAttentionStatus(String userid, String loginuserid) {
        MarketAttention marketAttention = marketAttentionMapper.selectAttentionStatus(userid, loginuserid);
        if (marketAttention==null) {
            return false;
        }
        return true;
    }

    @Override
    public int insertAttention(String userId, String loginUserid) {
        MarketAttention marketAttention = new MarketAttention();
        marketAttention.setId(KeyUtil.generateKey());
        marketAttention.setReceiveuserId(userId);
        marketAttention.setUserId(loginUserid);
        marketAttention.setDate(new Date());
        int i = marketAttentionMapper.insert(marketAttention);
        return i;
    }

    @Override
    public int deleteAttention(String userId, String loginUserid) {
        MarketAttention marketAttention = marketAttentionMapper.selectAttentionStatus(userId, loginUserid);
        int i = marketAttentionMapper.deleteByPrimaryKey(marketAttention.getId());
        return i;
    }
}
